package com.example.prom.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class DateUtil {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static boolean compareTime(String time) throws ParseException {
        Date parse = simpleDateFormat.parse(time);
        Date date = new Date();
        return date.compareTo(parse) > 0; // 当前时间大于传入则为ture
    }

    public static String  formatTimeStamp(Long timeStamp) throws ParseException {
        return Objects.isNull(timeStamp) ? "" : simpleDateFormat.format(new Date(timeStamp));
    }

    public static String  formatTimeStamp(Long timeStamp, String format) throws ParseException {
        return Objects.isNull(timeStamp) ? "" : new SimpleDateFormat(format).format(new Date(timeStamp));
    }

    public static void main(String[] args) throws ParseException {
        String text = "从From2022-01-01至To 2023-11-10 ";
        int from = text.indexOf("From");
        int to = text.indexOf("至To");
        System.out.println(from);
        System.out.println(to);
        String beginStr = text.substring(from + 4, to).trim();
        String endStr = text.substring(to + 3).trim();
        System.out.println(beginStr);
        System.out.println(endStr);
//        从From2022-01-01至To 2023-11-10 
        System.out.println(DateUtil.compareTime(beginStr));
        System.out.println(DateUtil.compareTime(endStr));
        System.out.println(DateUtil.compareTime("2022-07-25"));
    }
}
